//
//  PYFillTextCell.m
//  IHome
//
//   Created by zhujizheng on 2020/2/18.
//  Copyright © 2019年 com.zhujizheng. All rights reserved.
//

#import "PYFillTextCell.h"
#import "UITextField+MemoryLeak.h"

@interface PYFillTextCell () <UITextFieldDelegate>
@property (nonatomic, strong) UILabel *lbl;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIButton *btn;
@property (nonatomic, strong) UIView *bottomLine;
@property (nonatomic, strong) NSIndexPath *indexPath;
@property (nonatomic, weak) id<PYFillTextCellDelegate> delegate;
@end

@implementation PYFillTextCell

+ (instancetype)cellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath delegate:(nullable id<PYFillTextCellDelegate>)delegate
{
    PYFillTextCell *cell = [tableView dequeueReusableCellWithIdentifier:@"fillTextCellid"];
    if (!cell) {
        cell = [[PYFillTextCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"fillTextCellid"];
        [cell initLbl];
        [cell initTextField];
        [cell initBtn];
        [cell initBottomLine];
    }
    cell.indexPath = indexPath;
    cell.delegate = delegate;
    return cell;
}

#pragma mark - lbl
- (UILabel *)lbl
{
    if (!_lbl) {
        _lbl = [UILabel new];
        _lbl.textColor = PY_Dark_Color;
        _lbl.font = PY_Font_Regular(17);
    }
    return _lbl;
}

- (void)initLbl
{
    [self.contentView addSubview:self.lbl];
    [self.lbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.width.mas_equalTo(65);
        make.top.and.bottom.mas_equalTo(0);
    }];
}

#pragma mark - textField
- (UITextField *)textField
{
    if (!_textField) {
        _textField = [UITextField new];
        _textField.textColor = PY_Dark_Color;
        _textField.font = PY_Font_Regular(17);
        _textField.delegate = self;
    }
    return _textField;
}

- (void)initTextField
{
    [self.contentView addSubview:self.textField];
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.lbl.mas_right).mas_offset(10);
        make.right.mas_equalTo(-30);
        make.top.and.bottom.mas_equalTo(0);
    }];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if ([self.delegate respondsToSelector:@selector(cell:textDidChange:indexPath:)]) {
        [self.delegate cell:self textDidChange:text indexPath:self.indexPath];
    }
    return YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if ([self.delegate respondsToSelector:@selector(click:indexPath:)]) {
        [self.delegate click:self indexPath:self.indexPath];
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if ([self.delegate respondsToSelector:@selector(cell:textDidEnd:indexPath:)]) {
        [self.delegate cell:self textDidEnd:textField.text indexPath:self.indexPath];
    }
}

#pragma mark - btn
- (UIButton *)btn
{
    if (!_btn) {
        _btn = [UIButton new];
        _btn.hidden = YES;
    }
    return _btn;
}

- (void)initBtn
{
    [self.contentView addSubview:self.btn];
    [self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.textField);
    }];
    [self.btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
}

- (void)btnAction
{
    if ([self.delegate respondsToSelector:@selector(click:indexPath:)]) {
        [self.delegate click:self indexPath:self.indexPath];
    }
}

#pragma mark - bottomLine
- (UIView *)bottomLine
{
    if (!_bottomLine) {
        _bottomLine = [UIView new];
        _bottomLine.backgroundColor = [UIColor colorWithWhite:0 alpha:0.1];
    }
    return _bottomLine;
}

- (void)initBottomLine
{
    [self.contentView addSubview:self.bottomLine];
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.textField);
        make.right.mas_equalTo(self.textField);
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
    }];
}

@end
